<?php
include ('include/singleton-timer.php');

class Query {
	
	private static $result;
	private static $limiter;
	
	private static $timer = Singleton::get_instance;
	
	public function TEST_Q( $ss ) {
		$Q = "SELECT PUBCHEM_COMPOUND_CID ";
		$Q .= "FROM Compounds WHERE PUBCHEM_COMPOUND_CID IN (";
		$Q .= "SELECT CID FROM Compound_SMILES WHERE MATCH(PUBCHEM_OPENEYE_ISO_SMILES) ";
		$Q .= "AGAINST('". $ss ."' IN BOOLEAN MODE))";
		
		$timer->start_timer();
		$res = mysql_query($Q);
		$timer->end_timer();
		
		$result = Array ( $timer->get_time() ,  $res );
	}
	
	public function IUPAC_Query($ss, $lim) {		
		
		$rq = generate_range_query($lim);

		$Q = "SELECT * ";
		$Q .= "FROM Compounds WHERE ". $rq ." PUBCHEM_COMPOUND_CID IN (";
		$Q .= "SELECT CID FROM Compound_SMILES WHERE MATCH(PUBCHEM_OPENEYE_ISO_SMILES) ";
		$Q .= "AGAINST('". $ss ."' IN BOOLEAN MODE))";
		
		$timer->start_timer();
		$res = mysql_query($Q);
		$timer->end_timer();
		
		$result = Array ( $timer->get_time() ,  $res );
	}
	
	public function getResult() { return $result; }
	
	private function generate_range_query( $ary ) {
		$ranges = array();
		$size = count($ary);
		// check the lower, if there is a lower check upper
		// if lower and upper do BETWEEN
		// if only lower do GREATER THAN
		// otherwise do LESS THAN
		if (($size % 2) !== 0 ) {
			return ""; // don't apply anything since it will break
		}

		// make sure all values are numbers
		for ($n=0; $n < $size; $n++) {
			if (!(is_numeric($ary[$n]))) { $ary[$n] = ""; }
		}

		// make sure the lowerbound is greater than the upper
		for ($n=0; $n < $size; $n+=2) {
			if (($ary[$n] !== "") && ($ary[$n+1] !== "")) {
				if ($ary[$n] > $ary[$n+1]) {
					$tmp = $ary[$n+1];
					$ary[$n+1] = $ary[$n];
					$ary[$n] = $tmp;
				}
			}
		}

		$index=0;
		// Hydrogen Donors
		if ($ary[$index] !== "") {
			if ($ary[$index+1] !== "") {  
				$ranges[0] = "PUBCHEM_CACTVS_HBOND_DONOR BETWEEN " . $ary[$index] ."AND". $ary[$index+1] . " AND "; 
			}
			else {
				$ranges[0] = "PUBCHEM_CACTVS_HBOND_DONOR > " . $ary[$index] . " AND ";
			}
		}
		else if ($ary[$index+1] !== "") {
			$ranges[0] = "PUBCHEM_CACTVS_HBOND_DONOR < " . $ary[$index+1] . " AND "; 
		} 
		else {
			$ranges[0] = "";
		}

		// Hydrogen Acceptors
		$index += 2;
		if ($ary[$index] !== "") {
			if ($ary[$index+1] !== "") {  
				$ranges[1] = "PUBCHEM_CACTVS_HBOND_ACCEPTOR BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND ";
			}
			else {
				$ranges[1] = "PUBCHEM_CACTVS_HBOND_ACCEPTOR > " . $ary[$index] . " AND "; 
			}
		}
		else if ($ary[$index+1] !== "") {
			$ranges[1] = "PUBCHEM_CACTVS_HBOND_ACCEPTOR < " . $ary[$index+1] . " AND "; 
		} 
		else {
			$ranges[1] = "";
		}

		// Molecular Weight
		$index += 2;
		if ($ary[$index] !== "") {
			if ($ary[$index+1] !== "") {  
				$ranges[2] = "PUBCHEM_MOLECULAR_WEIGHT BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND "; 
			}
			else {
				$ranges[2] = "PUBCHEM_MOLECULAR_WEIGHT > " . $ary[$index] . " AND "; 
			}
		}
		else if ($ary[$index+1] !== "") {
			$ranges[2] = "PUBCHEM_MOLECULAR_WEIGHT < " . $ary[$index+1] . " AND "; 
		} 
		else {
			$ranges[2] = "";
		}

		// Log P
		$index += 2;
		if ($ary[$index] !== "") {
			if ($ary[$index+1] !== "") {  
				$ranges[3] = "PUBCHEM_XLOGP3_AA BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND "; 
			}
			else {
				$ranges[3] = "PUBCHEM_XLOGP3_AA > " . $ary[$index] . " AND "; 
			}
		}
		else if ($ary[$index+1] !== "") {
			$ranges[3] = "PUBCHEM_XLOGP3_AA < " . $ary[$index+1] . " AND "; 
		} 
		else {
			$ranges[3] = "";
		}

		// Rotatable Bonds
		$index += 2;
		if ($ary[$index] !== "") {
			if ($ary[$index+1] !== "") {  
				$ranges[4] = "PUBCHEM_CACTVS_ROTATABLE_BOND BETWEEN " . $ary[$index] ." AND ". $ary[$index+1] . " AND "; 
			}
			else {
				$ranges[4] = "PUBCHEM_CACTVS_ROTATABLE_BOND > " . $ary[$index] . " AND "; 
			}
		}
		else if ($ary[$index+1] !== "") {
			$ranges[4] = "PUBCHEM_CACTVS_ROTATABLE_BOND < " . $ary[$index+1] . " AND "; 
		} 
		else {
			$ranges[4] = "";
		}

		// ordered by ability to limit the query based on range and uniformity
		// logP, acceptor, rotatable, mol weight, donor,
		$rString = $ranges[3] . " " . $ranges[1] . " " . $ranges[4] . " " . $ranges[2] . " " . $ranges[0];

		if ($rString == "    ") { $rString = ""; }
		// remove trailing 'AND' and return string
		$pattern = '/[ ]+AND[ ]+$/';
		return 	preg_replace($pattern, "", $rString, 1);
	}
}
?>